package com.duo.controller.user;


import com.duo.dto.OrdersPageQueryDTO;
import com.duo.dto.OrdersPaymentDTO;
import com.duo.dto.OrdersSubmitDTO;
import com.duo.entity.Orders;
import com.duo.entity.User;
import com.duo.result.BaseResult;
import com.duo.result.PageResult;
import com.duo.service.OrderService;
import com.duo.vo.OrderSubmitVO;
import com.duo.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController("userOrderController")
@RequestMapping("/user/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 用户下单
     * @param ordersSubmitDTO
     * @return
     */
    @PostMapping("/submit")
    public Object submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO,
                         HttpServletRequest request){
        User user = (User) request.getAttribute("user");
        log.info("用户下单，参数为：{}",ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO, user);
        return BaseResult.success("用户下单成功", orderSubmitVO);
    }

    /**
     * 订单支付
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    public Object payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO,
                          HttpServletRequest request) {
        User user = (User) request.getAttribute("user");
        log.info("订单支付：{}", ordersPaymentDTO);
        Orders orders = orderService.payment(ordersPaymentDTO, user);
        log.info("生成预支付交易单：{}", orders);
        return BaseResult.success("订单支付成功", orders);
    }

    /**
     * 历史订单查询
     * @return
     */
    @PostMapping("/historyOrders")
    public Object page(@RequestBody OrdersPageQueryDTO ordersPageQueryDTO,
                       HttpServletRequest request) {
        User user = (User) request.getAttribute("user");
        PageResult pageResult = orderService.pageQuery4User(ordersPageQueryDTO, user);
        return BaseResult.success("历史订单查询成功", pageResult);
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @GetMapping("/orderDetail/{id}")
    public Object details(@PathVariable("id") Long id) {
        OrderVO orderVO = orderService.details(id);
        return BaseResult.success("查询成功", orderVO);
    }

    /**
     * 用户取消订单
     * @return
     */
    @PutMapping("/cancel/{id}")
    public Object cancel(@PathVariable("id") Long id) throws Exception {
        orderService.userCancelById(id);
        return BaseResult.success("取消订单成功");
    }


    /**
     * 客户催单
     * @param id
     * @return
     */
    @GetMapping("/reminder/{id}")
    public Object reminder(@PathVariable("id") Long id){
        orderService.reminder(id);
        return BaseResult.success("催单完成");
    }

}
